package com.m7.imkfsdk.chat.chatrow;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.m7.imkfsdk.R;
import com.m7.imkfsdk.chat.ChatActivity;
import com.m7.imkfsdk.chat.holder.BaseHolder;
import com.m7.imkfsdk.chat.holder.RichViewHolder;
import com.moor.imkf.model.entity.CardInfo;
import com.moor.imkf.model.entity.FromToMessage;
import com.moor.imkf.model.parser.HttpParser;

/**
 * Created by pangw on 2018/6/26.
 */

public class RichTxChatBox extends BaseChatRow {
    public RichTxChatBox(int type) {
        super(type);
    }

    @Override
    public boolean onCreateRowContextMenu(ContextMenu contextMenu, View targetView, FromToMessage detail) {
        return false;
    }

    @Override
    protected void buildChattingData(final Context context, BaseHolder baseHolder, final FromToMessage detail, int position) {
        RichViewHolder holder = (RichViewHolder) baseHolder;
        final FromToMessage message = detail;
        if(message != null) {
                final CardInfo ci = HttpParser.getCardInfo(message.cardInfo);
                holder.getWithdrawTextView().setVisibility(View.GONE);
                holder.getContainer().setVisibility(View.VISIBLE);

                holder.getTitle().setText(ci.title);
                holder.getContent().setText(ci.concent);
                holder.getName().setText(ci.name);

                if(ci.icon.equals("")){
                    holder.getImageView().setVisibility(View.GONE);
                }else{
                    holder.getImageView().setVisibility(View.VISIBLE);
                }
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.kf_pic_thumb_bg)
                    .error(R.drawable.kf_image_download_fail_icon)
                    .priority(Priority.HIGH)
                    .centerCrop();
                Glide.with(context).load(ci.icon)
                        .apply(options)
                        .into(holder.getImageView());
                holder.getKf_chat_rich_lin().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try{

/*Intent intent = new Intent();
                            intent.setAction("android.intent.action.VIEW");
                            Uri content_url = Uri.parse(ci.url);
                            intent.setData(content_url);
                            context.startActivity(intent);*/

                       /*
                       普通
                       https://www.villaday.com/villaDetail/q95oddQ8mk
                        http://m.villaday.com/house/detail/QVmgER2WZz
                        http://weixin.villaday.com/house/detail/QVmgER2WZz
                        特卖：
                        https://m.villaday.com/presell/detail/b0E80GRpE1
                        https://weixin.villaday.com/presell/detail/b0E80GRpE1*/
                            Log.e( "网址: ",ci.url );

                            if (ci.url.contains("https://www.villaday.com/villaDetail") ||
                                    ci.url.contains("http://m.villaday.com/house/detail") ||
                                    ci.url.contains("http://weixin.villaday.com/house/detail")||
                                    ci.url.contains("http://www.villaday.com/villaDetail") ||
                                    ci.url.contains("https://m.villaday.com/house/detail") ||
                                    ci.url.contains("https://weixin.villaday.com/house/detail")) {
                                //普通
                                String[] split = ci.url.split("\\/");

                                Intent intent = new Intent();
                                intent.putExtra("houseId", split[split.length - 1]);
                                intent.putExtra("persellId", "");
                                intent.setAction("com.villaday.idf.housedetailsactivityFor3");
                                //不加下面这行也行，因为intent的这个属性默认值即系Intent.CATEGORY_DEFAULT
                                intent.addCategory(Intent.CATEGORY_DEFAULT);

                                context.startActivity(intent);

                            } else if (ci.url.contains("https://m.villaday.com/presell/detail") ||
                                    ci.url.contains("https://weixin.villaday.com/presell/detail")||
                                    ci.url.contains("http://m.villaday.com/presell/detail") ||
                                    ci.url.contains("http://weixin.villaday.com/presell/detail")) {
                                //特卖
                                String[] split = ci.url.split("\\/");

                                Intent intent = new Intent();
                                intent.putExtra("houseId", split[split.length - 1]);
                                intent.putExtra("persellId", split[split.length - 1]);
                                intent.putExtra("presellProduct", true);
                                intent.setAction("com.villaday.idf.housedetailsactivityFor3");

                                //不加下面这行也行，因为intent的这个属性默认值即系Intent.CATEGORY_DEFAULT
                                intent.addCategory(Intent.CATEGORY_DEFAULT);

                                context.startActivity(intent);

                            } else {
                                Intent intent = new Intent();
                                intent.setAction("android.intent.action.VIEW");
                                Uri content_url = Uri.parse(ci.url);
                                intent.setData(content_url);
                                context.startActivity(intent);
                            }
                        }catch (Exception e){

                        }
                    }
                });
            View.OnClickListener listener = ((ChatActivity) context).getChatAdapter().getOnClickListener();
            getMsgStateResId(position, holder, message, listener);
        }
    }

    @Override
    public View buildChatView(LayoutInflater inflater, View convertView) {
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.kf_chat_row_rich_tx, null);
            RichViewHolder holder = new RichViewHolder(mRowType);
            convertView.setTag(holder.initBaseHolder(convertView, true));
        }
        return convertView;
    }

    @Override
    public int getChatViewType() {
        return ChatRowType.RICHTEXT_ROW_TRANSMIT.ordinal();
    }
}
